<template>
  <div class="system-user-dialog-container">
    <el-dialog v-model="state.dialog.isShowDialog" :title="state.dialog.title" width="1200px">
      <el-form ref="userDialogFormRef" :model="state.ruleForm" :rules="rules" label-width="100px" size="default">
        <el-row :gutter="35">
          <el-divider>
            <el-icon>
              <ele-SwitchFilled/>
            </el-icon>
            基础信息
          </el-divider>
          <el-col :span="8" class="mb20">
            <el-form-item label="商品名称" prop="name">
              <el-input v-model="state.ruleForm.name" :disabled="formReadonly"
                        placeholder="商品名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="8" class="mb20">
            <el-form-item label="商品条码" prop="barcode">
              <el-input v-model="state.ruleForm.barcode" :min="0" controls-position="right" placeholder="商品条码"/>
            </el-form-item>
          </el-col>
          <el-col :span="8" class="mb20">
            <el-form-item label="商品货号" prop="artNo">
              <el-input v-model="state.ruleForm.artNo" :min="0" controls-position="right" placeholder="商品货号"/>
            </el-form-item>
          </el-col>

          <el-col :span="8" class="mb20">
            <el-form-item label="商品分类" prop="category">
              <el-select v-model="state.ruleForm.category"
                         clearable
                         placeholder="请选择操作类型"
                         size="default">
                <el-option
                    v-for="item in state.categoryList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8" class="mb20">
            <el-form-item label="商品品牌" prop="brandId">
              <el-select v-model="state.ruleForm.brandId"
                         clearable
                         placeholder="请选择品牌"
                         size="default">
                <el-option
                    v-for="item in state.supplierList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8" class="mb20">
            <el-form-item label="库存预警" prop="stockWarning">
              <el-input-number v-model="state.ruleForm.stockWarning" :min="0" controls-position="right"
                               placeholder="库存预警"/>
            </el-form-item>
          </el-col>
          <!--          <el-col :span="8" class="mb20">
                      <el-form-item label="规格型号" prop="spec">
                        <el-input v-model="state.ruleForm.spec" :min="0" controls-position="right" placeholder="规格"/>
                      </el-form-item>
                    </el-col>-->
          <el-col :span="8" class="mb20">
            <el-form-item label="单位" prop="unit">
              <el-select v-model="state.ruleForm.unit" :disabled="formReadonly" class="w100" clearable
                         placeholder="请选择">
                <el-option
                    v-for="item in state.unitList"
                    :key="item.dictValue"
                    :label="item.dictLabel"
                    :value="item.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-divider>
            <el-icon>
              <ele-PriceTag/>
            </el-icon>
            商品价格
          </el-divider>
          <el-col :span="12" class="mb20">
            <el-form-item label="热门服务" prop="isHot">
              <el-select v-model="state.ruleForm.isHot"
                         clearable
                         placeholder="请选择"
                         size="default">
                <el-option
                    v-for="item in state.sysYesNo"
                    :key="item.dictValue"
                    :label="item.dictLabel"
                    :value="item.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12"></el-col>
          <el-col :span="12" class="mb20">
            <el-form-item label="是否积分" prop="isPoint">
              <el-select v-model="state.ruleForm.isPoint"
                         clearable
                         placeholder="请选择"
                         size="default">
                <el-option
                    v-for="item in state.sysYesNo"
                    :key="item.dictValue"
                    :label="item.dictLabel"
                    :value="item.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb20">
            <el-form-item label="积分数量" prop="point">
              <el-input-number v-model="state.ruleForm.point" :min="0" controls-position="right"
                               placeholder="积分数量"/>
            </el-form-item>
          </el-col>

          <el-col :span="12" class="mb20">
            <el-form-item label="积分换购" prop="isExchange">
              <el-select v-model="state.ruleForm.isExchange"
                         clearable
                         placeholder="请选择"
                         size="default">
                <el-option
                    v-for="item in state.sysYesNo"
                    :key="item.dictValue"
                    :label="item.dictLabel"
                    :value="item.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb20">
            <el-form-item label="换购积分" prop="exchangePoint">
              <el-input-number v-model="state.ruleForm.exchangePoint" :min="0" controls-position="right"
                               placeholder="换购积分"/>
            </el-form-item>
          </el-col>

          <el-col :span="12" class="mb20">
            <el-form-item label="提成类型" prop="commissionType">
              <el-select v-model="state.ruleForm.commissionType"
                         clearable
                         placeholder="请选择"
                         size="default">
                <el-option
                    v-for="item in state.commissionList"
                    :key="item.dictValue"
                    :label="item.dictLabel"
                    :value="item.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="mb20">
            <el-form-item label="提成金额" prop="commissionAmount">
              <el-input-number v-model="state.ruleForm.commissionAmount" :min="0"
                               controls-position="right" placeholder="提成金额"/>
            </el-form-item>
          </el-col>

          <el-col :span="8" class="mb20">
            <el-form-item label="成本价" prop="costPrice">
              <el-input-number v-model="state.ruleForm.costPrice" :min="0" controls-position="right"
                               placeholder="成本价"/>
            </el-form-item>
          </el-col>
          <el-col :span="8" class="mb20">
            <el-form-item label="销售价" prop="salePrice">
              <el-input-number v-model="state.ruleForm.salePrice" :min="0" controls-position="right"
                               placeholder="销售价"/>
            </el-form-item>
          </el-col>
          <el-col :span="8" class="mb20">
            <el-form-item label="会员价" prop="vipPrice">
              <el-input-number v-model="state.ruleForm.vipPrice" :min="0" controls-position="right"
                               placeholder="会员价"/>
            </el-form-item>
          </el-col>
          <el-col :span="8" class="mb20">
            <el-form-item label="划线价" prop="scribePrice">
              <el-input-number v-model="state.ruleForm.scribePrice" :min="0" controls-position="right"
                               placeholder="划线价"/>
            </el-form-item>
          </el-col>
          <el-divider>
            <el-icon>
              <ele-Picture/>
            </el-icon>
            介绍信息
          </el-divider>
          <el-col :span="24" class="mb20">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="state.ruleForm.remark" :disabled="formReadonly" maxlength="150"
                        placeholder="请输入备注"
                        type="textarea"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
				<span class="dialog-footer">
					<el-button size="default" @click="onCancel">取 消</el-button>
					<el-button size="default" type="primary" @click="onSubmit(userDialogFormRef)">
						{{ state.dialog.submitTxt }}
					</el-button>
				</span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" name="systemUserDialog" setup>
import {computed, nextTick, onMounted, reactive, ref} from 'vue';
import type {FormInstance, FormRules} from 'element-plus';
import {ElMessage} from 'element-plus';
import {dictDataApi} from "/@/api/sys/dictData";
import {commodityApi} from "/@/api/store/commodity";
import {commodityCategoryApi} from "/@/api/store/commodity/category/commodity";
import {supplierApi} from "/@/api/store/commodity/supplier";
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const userDialogFormRef = ref();
const state = reactive({
  ruleForm: {
    name: '', // 分类名称
    barcode: '',
    artNo: '',
    brandId: '',
    category: '',
    spec: '',
    specIds: '',
    unit: '',
    costPrice: '',
    salePrice: '',
    vipPrice: '',
    isHot: '',
    isPoint: '',
    isExchange: '',
    commissionType: '',
    point: '',
    exchangePoint: '',
    stockWarning: 10,
    scribePrice: '',
    commissionAmount: '',
    remark: '', // 备注
  },
  dialog: {
    isShowDialog: false,
    type: '',
    title: '',
    submitTxt: '',
  },
  unitList: [],
  categoryList: [],
  commissionList: [],
  sysYesNo: [],
  supplierList: [],
});
const rules = reactive<FormRules>({
  name: [{required: true, message: '不能为空', trigger: 'blur'}],
  artNo: [{required: true, message: '不能为空', trigger: 'blur'}],
  category: [{required: true, message: '不能为空', trigger: 'blur'}],
  isHot: [{required: true, message: '不能为空', trigger: 'blur'}],
  isPoint: [{required: true, message: '不能为空', trigger: 'blur'}],
  isExchange: [{required: true, message: '不能为空', trigger: 'blur'}],
  commissionType: [{required: true, message: '不能为空', trigger: 'blur'}],
  vipPrice: [{required: true, message: '不能为空', trigger: 'blur'}],
  barcode: [{required: true, message: '不能为空', trigger: 'blur'}],
  costPrice: [{required: true, message: '不能为空', trigger: 'blur'}],
  scribePrice: [{required: true, message: '不能为空', trigger: 'blur'}],
  salePrice: [{required: true, message: '不能为空', trigger: 'blur'}],
});
// 打开弹窗
const openDialog = (type: string, row: RowUserType) => {
  state.dialog.type = type;
  state.dialog.isShowDialog = true;
  if (type === 'edit') {
    state.dialog.title = '修改商品';
    state.dialog.submitTxt = '修 改';
  } else if (type === 'add') {
    state.dialog.title = '新增商品';
    state.dialog.submitTxt = '新 增';
  }
  nextTick(() => {
    if (type === 'add') {
      // 清空表单，此项需加表单验证才能使用
      userDialogFormRef.value.resetFields();
    } else {
      state.ruleForm = Object.assign(state.ruleForm, row);
    }
  });
};
// 关闭弹窗
const closeDialog = () => {
  state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
  closeDialog();
};

// 提交
const onSubmit = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      if (state.dialog.type === 'add') {
        commodityApi()
            .addRecord(state.ruleForm)
            .then((res) => {
              if (res && res.code === 200) {
                ElMessage.success(res.msg || '新增成功');
                closeDialog();
                emit('refresh');
              }
            });
      } else if (state.dialog.type === 'edit') {
        commodityApi()
            .updateRecord(state.ruleForm)
            .then((res) => {
              if (res && res.code === 200) {
                ElMessage.success(res.msg || '修改成功');
                closeDialog();
                emit('refresh');
              }
            });
      }
    }
  });
};

// 页面加载时
onMounted(() => {
  // 操作类型
  dictDataApi()
      .getDictByType('commodity_unit')
      .then(res => {
        state.unitList = res.data;
      })

  // 是否
  dictDataApi()
      .getDictByType("sys_yes_no")
      .then(res => {
        state.sysYesNo = res.data;
      })

  // 商品分类
  commodityCategoryApi()
      .findAll()
      .then(res => {
        state.categoryList = res.data;
      })

  // 商品品牌
  supplierApi()
      .findAll()
      .then(res => {
        state.supplierList = res.data;
      })

  // 提成类型
  dictDataApi()
      .getDictByType("commission_type")
      .then(res => {
        state.commissionList = res.data;
      })
});

const formReadonly = computed(() => {
  return state.dialog.type !== 'add' && state.dialog.type !== 'edit'
})
// 暴露变量
defineExpose({
  openDialog,
});
</script>
